package engine.net.mgr
{
	import engine.data.GamePlayer;
	import engine.data.ModelLocator;
	import engine.data.ObjectData;
	import engine.data.Record;
	import engine.data.SceneObject;
	import engine.data.View;
	import engine.data.ViewObject;
	import engine.event.BaseNetEvent;
	import engine.flag.GameDefine;
	import engine.logic.ActSee;
	import engine.logic.AlertLogic;
	import engine.net.NetSocket;
	import engine.net.datatype.ByteArrayEx;
	import engine.net.datatype.WString;
	import engine.net.event.CustomMsgEvent;
	import engine.net.event.MsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.SystemMsgFlag;
	import engine.net.utils.NetDataType;
	import engine.ui.AlertUI;
	
	import flash.geom.Point;
	import flash.utils.getTimer;
	
	import newx.BaseObject;
	import newx.utils.debug.IO;
	
	public class SystemReturn
	{
		private var model:ModelLocator;
		
		public static var _self:SystemReturn;
		public static var _isFirst	:Boolean 	=  true;		
		
		/**是否记录调试信息*/
		public static var _isShowLog	:Boolean	=	false;
		
		public static var time		:int;
		
		public function SystemReturn()
		{
			if(_self != null)
			{
				throw new Error("SystemReturn is Listening ...");
			}
			model	=	ModelLocator.getInstance();
			addEvent();
		}
		
		public static function StartListener():void
		{
			if(_self==null) _self	=	new SystemReturn();
			
		}
		
		private function addEvent():void
		{
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_LOGIN_SUCCEED)	,ServerLoginSucceed);
			
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_ERROR_CODE)	,ServerErrorCode);
			
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_QUEUE)			,ServerQueue);
			
			//更新属性名总表
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_PROPERTY_TABLE),ServerPropertyTable);
			
			//更新表名名总表
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_RECORD_TABLE),ServerRecordTable);
			
			//引擎通知客户端进入场景，这时不发场景上数据对象的，等待客户端发送SystemSender.getInstance().ClientReady();,接下来服务器推送场景上的obj数据
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_ENTRY_SCENE),ServerEntrySceneEx);
			
			//更新场景上的属性
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_SCENE_PROPERTY),ServerSceneProperty);
			
			//添加场景obj（根据类型区分，并记录在场景对象上，如Doors，Players）
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_ADD_OBJECT),ServerAddObj);
			
			//更新obj上的属性（通用）,同步更新主角和其他类型obj
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_OBJECT_PROPERTY),ServerObjectProperty);
			
			//删除场景obj
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_REMOVE_OBJECT),ServerRemoveObject);
			
			//主角身上创建新视图
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_CREATE_VIEW),ServerCreateView);
			
			//主角身上删除新视图
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_DELETE_VIEW),ServerDeleteView);
			
			//主角身上视图属性更新
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_VIEW_PROPERTY),ServerViewProperty);
			
			//主角身上视图添加新对象（子对象）
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_VIEW_ADD),ServerViewAdd);
			
			//主角身上视图删除新对象（子对象）
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_VIEW_REMOVE),ServerViewRemove);
			
			//主角身上视图更新新对象（子对象）
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_VIEWOBJ_PROPERTY),ServerViewObjProperty);
			
			//主角身上视图对象（子对象）与另一视图对象交换。
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_VIEW_EXCHANGE),ServerViewExchange);			
			
			//玩家身上表格相应消息
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_RECORD_ADDROW),ServerRecordAddRow);
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_RECORD_DELROW),ServerRecordDelRow);			
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_RECORD_GRID),ServerRecordGrid);
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_RECORD_CLEAR),ServerRecordClear);	
			
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_CUSTOM)	,ServerCustom);//消息头21
			
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_FROM_GM)	,ServerFromGM);
			
			//系统提示
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_SYSTEM_INFO_NEW),ServerAlert);		
			
			//菜单维护
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_MENU_NEW),ServerMenu);
			NetSocket.getInstance().addEventListener(String(SystemMsgFlag.SERVER_MENU_CLEAR),ServerMenuClear);
		}
		
		//消息事件函数
		private function ServerLoginSucceed(e:MsgEvent):void
		{			
			var		msg:ByteArrayEx	=	e._msgData;
			var		result:uint	=	model.player.LoginSucceed(msg);
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_LOGIN_SUCCEED("+e.type+")],参数：result:"+result);
			
			//登录时需要选择角色，参数为false
			model.dispatchEvent(new BaseNetEvent(BaseNetEvent.LOGIN_SUCCESS,result));
		}
		private function ServerErrorCode(e:MsgEvent):void
		{			
			var	msg:ByteArrayEx		= e._msgData;
			var errorcode:uint		= msg.readInt();
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_ERROR_CODE("+e.type+")],errorcode:"+errorcode);
			
			model.dispatchEvent(new BaseNetEvent(BaseNetEvent.SERVER_ERROR,errorcode));
		}
		
		private function ServerQueue(e:MsgEvent):void
		{			
			var	msg:ByteArrayEx		= e._msgData;
			
			//队列编号
			var nQueue		:int			=	msg.readInt();
			
			// 在队列中的位置（为0表示结束排队）
			var nPosition	:int			=	msg.readInt();
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_QUEUE("+e.type+")]（排队登陆消息）,nPosition:"+nPosition);
			model.dispatchEvent(new BaseNetEvent(BaseNetEvent.SERVER_QUEUE,nQueue,nPosition));
		}
		
		//获得属性表
		private function ServerPropertyTable(e:MsgEvent):void
		{			
			ActSee.PostLoginStatus(22002,1,getTimer() - time);
			
			var msg:ByteArrayEx	= e._msgData;
			var lenth:Number	= msg.length;
			var count:uint		= msg.readUnsignedShort();
			//设置全局属性表 
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_PROPERTY_TABLE("+e.type+")]，lenth："+lenth+",count:"+count+",参数信息：");
			
			var i:Number = 0;
			for (i = 0; i < count; i++)
			{
				var proName:String	= msg.readMultiByteEx();	
				var proType:uint	= msg.readUnsignedByte();
				
				ObjectData.ProertyNames[i]	=	proName;
				ObjectData.ProertyTypes[i]	=	proType;
			}
		}
		
		//获得表格表
		private function ServerRecordTable(e:MsgEvent):void
		{
			var msg:ByteArrayEx	= e._msgData;
			var lenth:Number	= msg.length;
			var count:uint		= msg.readUnsignedShort();
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_RECORD_TABLE("+e.type+")]，lenth："+lenth+",count:"+count+",参数信息：");
			//设置全局表格表 
			model.scene.SetRecordTable(msg , count);
		}

		//进入场景
		private function ServerEntrySceneEx(e:MsgEvent):void
		{
//			//进入新场景，干掉旧场景中的obj，保持回调
//			model.scene.Clear();
			
			var msg:ByteArrayEx	= e._msgData;
			
			// 动态密匙
			var nDynamicKey:uint		= msg.readInt();
			
			// 主角玩家的对象号
			var	ObjectId:Point			= new Point;
			ObjectId.x					= msg.readInt();
			ObjectId.y					= msg.readInt();
			
			// 场景模型号
			var nSceneModelId:uint		= msg.readInt();
					
//			model.scene.ResetRole(ObjectId);	
			model.scene.Role.ObjID		= ObjectId;
						
			if (_isFirst)
			{
				//登录时不需创建角色，参数为true
				model.dispatchEvent(new BaseNetEvent(BaseNetEvent.LOGIN_SUCCESS,GamePlayer.LoginType_LoginRole));	
				_isFirst = false;
			}
			
			SystemSender.getInstance().ClientReady();
//			
//			SceneManager.getInstance().enterScene(nSceneModelId);
		}
		
//		//进入场景
//		private function ServerEntryScene(e:MsgEvent):void
//		{			
//			//进入新场景，干掉旧场景中的obj，保持回调
//			model.scene.Clear();
//			
//			var msg:ByteArrayEx	= e._msgData;
//			
//			// 动态密匙
//			var nDynamicKey:uint		= msg.readInt();
//			
//			// 主角玩家的对象号
//			var	ObjectId:Point			= new Point;
//			ObjectId.x					= msg.readInt();
//			ObjectId.y					= msg.readInt();
//			
//			// 场景模型号
//			var nSceneModelId:uint		= msg.readInt();
//					
//			model.scene.ResetRole(ObjectId);		
//						
//			if (_isFirst)
//			{
//				//登录时不需创建角色，参数为true
//				model.dispatchEvent(new BaseNetEvent(BaseNetEvent.LOGIN_SUCCESS,GamePlayer.LoginType_LoginRole));	
//				_isFirst = false;
//			}
//			
//			if(_isShowLog)
//				IO.traceCommand("    ↓		收到引擎消息[ServerEntryScene("+e.type+")]，nDynamicKey："+
//				nDynamicKey+",ObjectId:("+ObjectId.x+","+ObjectId.y+"),nSceneModelId："+nSceneModelId);
//			
//			//此时不加载场景，等到自定义的那条消息来再加载
////			model.dispatchEvent(new BaseNetEvent(BaseNetEvent.ENTRY_SCENE));	
//			// 向服务端发送Ready消息
//			SystemSender.getInstance().ClientReady();
//		}
		//更新场景属性
		private function ServerSceneProperty(e:MsgEvent):void
		{
			var msg:ByteArrayEx	= e._msgData;
			var lenth:Number	= msg.length;
			var count:uint		= msg.readUnsignedShort();			
			
			//更新属性
			var i:uint = 0;
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_SCENE_PROPERTY("+e.type+")](更新场景属性)，count："+count+",更新信息：");
			
			model.scene.setPropertiesFormByteArrayEx(count,msg)
		}
		//更新对象属性
		private function ServerObjectProperty(e:MsgEvent):void
		{
			var msg:ByteArrayEx	= e._msgData;
			var lenth:Number	= msg.length;
			
			var objID:Point		= msg.readObjectEx();						
			var count:uint		= msg.readUnsignedShort();
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_OBJECT_PROPERTY("+e.type+")](更新场景子对象属性),objID:("+objID.x+","+objID.y+")");
			
			if(objID.equals(model.scene.Role.ObjID))
			{
				var modelId : int = model.scene.Role.ModelId;
				//主角
				model.scene.Role.setPropertiesFormByteArrayEx(count, msg);
				return;
			}
			
			var tobj:SceneObject	=	model.scene.GetObject(objID);
			if(tobj == null)
			{
				//对象不存在
				
				if(_isShowLog)
					IO.traceCommand("属性更新失败，对象不存在。",objID.x,objID.y)
				
				return ;
			}
			//更新属性
			tobj.setPropertiesFormByteArrayEx(count, msg);
		}	
		
		/**
		 *	移除obj 
		 * @param e
		 * 
		 */		
		private function ServerRemoveObject(e:MsgEvent):void
		{			
			//UNIQUE_ID	ObjectId;	// 唯一标识
			var msg:ByteArrayEx	= e._msgData;
			var objID:Point		= msg.readObjectEx();						
		
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_REMOVE_OBJECT("+e.type+")](删除场景子对象),objID:("+objID.x+","+objID.y+")");
			
			model.scene.RemoveObj(objID);
		}	
		
		//添加对象
		private function ServerAddObj(e:MsgEvent):void
		{
			var msg:ByteArrayEx	= e._msgData;
			
			var lenth:Number = msg.length;
				
			var objID:Point		= msg.readObjectEx();
			var objType:uint	= msg.readUnsignedShort();
			var objModelID:int	= msg.readInt();
			
			var count:uint		= msg.readShort();
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_ADD_OBJECT("+e.type+")](添加场景子对象),objID:("+
					objID.x+","+objID.y+")"+",objType:"+objType+",objModelID:"+objModelID);
			
			model.scene.AddObj(objID,objType,objModelID,count,msg);
		}	
		//创建视图
		private function ServerCreateView(e:MsgEvent):void
		{
//			_UI16	nViewId;		// 视窗编号, 0-保留给场景
//			_UI16	nType;			// 类型
//			_UI16	nCapacity;		// 大小
			var msg:ByteArrayEx	= e._msgData;
			
			var lenth:Number = msg.length;			
			var nViewId:uint	= msg.readUnsignedShort();
			var nType:uint		= msg.readUnsignedShort();
			var nCapacity:uint	= msg.readUnsignedShort();
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_CREATE_VIEW("+e.type+")](创建视图),nViewId:"+nViewId+
					",nType"+nType+",nCapacity:"+nCapacity);
			
			//视图名临时这么写
			var	viewName:String	= "View" + String(nViewId);
			var	view:View	=	new View(nViewId,nType,nCapacity);
			model.scene.HandlePropertiesEx(viewName,view);	
			
			model.dispatchEvent(new BaseNetEvent(BaseNetEvent.VIEW_ADD,viewName));
		}
		//删除视图
		private function ServerDeleteView(e:MsgEvent):void
		{
			//			_UI16	nViewId;		// 视窗编号, 0-保留给场景
			//			_UI16	nType;			// 类型
			//			_UI16	nCapacity;		// 大小
			var msg:ByteArrayEx	= e._msgData;
			
			var lenth:Number = msg.length;			
			var nViewId:uint	= msg.readUnsignedShort();
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_DELETE_VIEW("+e.type+")](删除视图),nViewId:"+nViewId);
			
			//视图名临时这么写
			var	viewName:String	= "View" + String(nViewId);
			if(model.scene.CheckProperties(viewName))
			{
				model.dispatchEvent(new BaseNetEvent(BaseNetEvent.VIEW_REMOVE,viewName));
				model.scene.DeletePropertiesEx(viewName);				
			}
		}
		//视图增加
		private function ServerViewAdd(e:MsgEvent):void
		{
//			_UI16	nViewId;		// 视窗编号
//			_UI32	nObjectId;		// 视窗对象号
//			_UI16	nCount;			// 后续属性数量
			var msg:ByteArrayEx	= e._msgData;
			
						
			var nViewId:uint	= msg.readUnsignedShort();
			var nObjectId:uint	= msg.readInt();
			var nCount:uint		= msg.readUnsignedShort();
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_VIEW_ADD("+e.type+")](添加视图子对象),nViewId:"+nViewId+
					",nObjectId:"+nObjectId+",nCount:"+nCount);
			
			//视图名临时这么写
			var	viewName:String	= "View" + String(nViewId);
			if(model.scene.CheckProperties(viewName))
			{
				var	view:View	=	model.scene.GetProperties(viewName);
				view.ViewAdd(nObjectId,nCount,msg);
			}
		}
		//视图属性
		private function ServerViewProperty(e:MsgEvent):void
		{
//			_UI16	nViewId;		// 视窗编号
//			_UI16	nCount;			// 后续属性数量
			var msg:ByteArrayEx	= e._msgData;			
			
			var nViewId:uint	= msg.readUnsignedShort();
			var nCount:uint		= msg.readUnsignedShort();
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_VIEW_PROPERTY("+e.type+")](视图属性更新),nViewId:"+nViewId+
					",nCount:"+nCount);
			
			//视图名临时这么写
			var	viewName:String	= "View" + String(nViewId);
			if(model.scene.CheckProperties(viewName))
			{
				var	view:View	=	model.scene.GetProperties(viewName);
				
				view.setPropertiesFormByteArrayEx(nCount, msg);
			}
		}
		
		//视图中对象属性修改
		private function ServerViewObjProperty(e:MsgEvent):void
		{
//			_UI16	nViewId;		// 视窗编号
//			_UI32	nObjectId;		// 视窗对象号
//			_UI16	nCount;			// 后续属性数量
//			// 属性数据
//			// 后续其他的属性，根据属性表进行解码
			var msg:ByteArrayEx	= e._msgData;			
			
			var nViewId:uint	= msg.readUnsignedShort();
			var nObjectId:uint	= msg.readInt();
			var nCount:uint		= msg.readUnsignedShort();
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_VIEWOBJ_PROPERTY("+e.type+")](视图子对象属性更新),nViewId:"+nViewId+
					",nObjectId:"+nObjectId+",nCount:"+nCount);
			
			//视图名临时这么写
			var	viewName:String	= "View" + String(nViewId);
			if(model.scene.CheckProperties(viewName))
			{
				var	view:View	=	model.scene.GetProperties(viewName);
				view.ViewObjProperty(nObjectId,nCount,msg);
			}
		}
		
		private function ServerViewExchange(e:MsgEvent):void
		{
//			_UI16	nViewId1;		// 视窗编号1
//			_UI32	nObjectId1;		// 视窗对象号1
//			_UI16	nViewId2;		// 视窗编号1
//			_UI32	nObjectId2;		// 视窗对象号1
			var msg:ByteArrayEx	= e._msgData;			
			
			var nViewId1:uint	= msg.readUnsignedShort();
			var nObjectId1:uint	= msg.readInt();
			var nViewId2:uint	= msg.readUnsignedShort();
			var nObjectId2:uint	= msg.readInt();
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_VIEW_EXCHANGE("+e.type+")](视图交换消息),nViewId1:"+nViewId1+
					",nObjectId1:"+nObjectId1+"nViewId2:"+nViewId2+",nObjectId2:"+nObjectId2);
			
			//视图名临时这么写
			var	viewName1:String	= "View" + String(nViewId1);
			var	viewName2:String	= "View" + String(nViewId2);
			
			if(model.scene.CheckProperties(viewName1) && 
				model.scene.CheckProperties(viewName2))
			{
				//源位置肯定有数据
				var	view1:View	=	model.scene.GetProperties(viewName1);
				var	tobj:ViewObject		=	null;
				if(view1.CheckProperties(String(nObjectId1)))
				{
					tobj		=	view1.GetProperties(String(nObjectId1));
				}
				
				var	view2:View	=	model.scene.GetProperties(viewName2);
				if(!view2.CheckProperties(String(nObjectId2)))
				{
					//目标位置没有对象,给目标位置更新数据，现位置删数据								
					view1.DeletePropertiesEx(String(nObjectId1));
					tobj.ObjKey	=	String(nObjectId2);
					tobj.parent	=	view2;
					view2.HandlePropertiesEx(String(nObjectId2),tobj);	
				}
				else
				{
					var		tobj2:*		=	view2.GetProperties(String(nObjectId2));
					//目标位置有数据，要交换
					tobj.ObjKey		=	String(nObjectId2);
					//修改parent
					tobj.parent			=	view2;
					
					//view2.DeletePropertiesEx(String(nObjectId2));					
					view2.HandlePropertiesEx(String(nObjectId2),tobj);
					tobj2.ObjKey	=	String(nObjectId1);
					
					tobj2.parent		=	view1;
					//view2.DeletePropertiesEx(String(nObjectId1));
					view1.HandlePropertiesEx(String(nObjectId1),tobj2);				
				}
			}					
		}
		
		
		//视图移除对象
		private function ServerViewRemove(e:MsgEvent):void
		{
			//			_UI16	nViewId;		// 视窗编号
			//			_UI32	nObjectId;		// 视窗对象号
			var msg:ByteArrayEx	= e._msgData;			
						
			var nViewId:uint	= msg.readUnsignedShort();
			var nObjectId:uint	= msg.readUnsignedShort();			
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_VIEW_REMOVE("+e.type+")](删除视图子对象),nViewId:"+nViewId+
					",nObjectId:"+nObjectId);
			
			//视图名临时这么写
			var	viewName:String	= "View" + String(nViewId);
			if(model.scene.CheckProperties(viewName))
			{
				var	view:View	=	model.scene.GetProperties(viewName);
				view.ViewRemove(nObjectId);	
			}
		}	
		
		
		//表格添加行
		private function ServerRecordAddRow(e:MsgEvent):void
		{
			//			_UI16	nIndex;		// 表序号
			//			_UI16	nRow;		// 插入的行号
			//			_UI16	nRows;		// 行数
			var msg:ByteArrayEx	= e._msgData;
			
			var lenth:Number = msg.length;			
			var nIndex:uint	= msg.readUnsignedShort();
			var nRow:uint	= msg.readUnsignedShort();
			var nRows:uint	= msg.readUnsignedShort();
			
			var	recName:String	=	model.scene.GetRecordName(nIndex);
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_RECORD_ADDROW("+e.type+")](表格添加行),lenth:"+lenth+
					",nIndex:"+nIndex+",nRow:"+nRow+",nRows:"+nRows);
			
			if(!model.scene.CheckProperties(recName))
			{	
				//表格不存在
				if(_isShowLog)
					IO.traceCommand("不存在名为["+recName+"]表格！表格数据添加失败。");
				return ;
			}
			var	record:Record	=	model.scene.GetRecordEx(recName);
			
			record.AddRow(nRow,nRows,msg);
		}
		/**
		 * 删除表格行 
		 * @param e
		 * 
		 */		
		private function ServerRecordDelRow(e:MsgEvent):void
		{
			//			_UI16	nIndex;		// 表序号
			//			_UI16	nRow;		// 删除的行号
			
			var msg:ByteArrayEx	= e._msgData;
			var nIndex:uint	= msg.readUnsignedShort();
			var nRow:uint	= msg.readUnsignedShort();
			
			var	recName:String	=	model.scene.GetRecordName(nIndex);
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_RECORD_DELROW("+e.type+")](表格删除行),nIndex:"+nIndex+
					",nRow:"+nRow+",recName:"+recName);
			
			if(!model.scene.CheckProperties(recName))
			{	
				//表格不存在
				if(_isShowLog)
					IO.traceCommand("不存在名为["+recName+"]表格！表格数据删除失败。");
				return ;
			}
			var	record:Record	=	model.scene.GetRecordEx(recName);		
			record.DelRow(nRow);
		}
		
		//表数据改变
		private function ServerRecordGrid(e:MsgEvent):void
		{
			//			_UI16	nIndex;		// 表序号
			//			_UI16	nCount;		// 数据数量
			//			//_UI16 nRow
			//			//_UI08 nCol		
			//			// 数据
			var msg:ByteArrayEx	= e._msgData;
			var nIndex:uint	= msg.readUnsignedShort();
			var nCount:uint	= msg.readUnsignedShort();
			
			var	recName:String	=	model.scene.GetRecordName(nIndex);
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_RECORD_GRID("+e.type+")](表格修改行),nIndex:"+nIndex+
					",nCount:"+nCount+",recName:"+recName);
			
			if(!model.scene.CheckProperties(recName))
			{	
				//表格不存在
				if(_isShowLog)
					IO.traceCommand("不存在名为["+recName+"]表格！表格数据修改失败。");
				return ;
			}
			var	record:Record	=	model.scene.GetRecordEx(recName);		
			record.ChangeRecordGrid(nCount,msg);
		}
		
		//表数据清空
		private function ServerRecordClear(e:MsgEvent):void
		{
			//_UI16	nIndex;		// 表序号
			var msg:ByteArrayEx	= e._msgData;
			var nIndex:uint	= msg.readUnsignedShort();
			var	recName:String	=	model.scene.GetRecordName(nIndex);
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_RECORD_CLEAR("+e.type+")](清除表格数据),nIndex:"+nIndex+
					",recName:"+recName);
			
			if(!model.scene.CheckProperties(recName))
			{	
				//表格不存在
				if(_isShowLog)
					IO.traceCommand("不存在名为["+recName+"]表格！表格数据清空失败。");
				return ;
			}
			var	record:Record	=	model.scene.GetRecordEx(recName);		
			record.RecordClear();
		}
		
		//自定义消息
		private function ServerCustom(e:MsgEvent):void
		{
//			UNIQUE_ID	ObjectId;	// 对象唯一标识
//			ASYNC_ID	AsyncId;	// 异步事件标识
//			_UI16		nArgNum;	// 参数数量
//			//_UI08		nArgType;	// 参数类型
//			// 参数数据
			
			var msg:ByteArrayEx	= e._msgData;
			if(msg.length<=20)
			{
				//消息不完整，自定义消息没有任何数据，因为自定义消息的最短长度应该大于20
				msg.position	=	msg.length;
				AlertUI.showAlert("错误","自定义消息没有数据");
				return;
			}
			
			var ObjectId:Point		= msg.readObjectEx();
			var AsyncId:Point		= msg.readObjectEx();
			var nArgNum:uint		= msg.readUnsignedShort();	
			
			var	type:uint			= msg.readByte();
			
			 
			var	command:uint		= msg.GetDataByType(type);
			
			var i:uint			= 0;
			var	 ArgValue:Array	= new Array();
			
			var showmsg:String	=	"";
			for(i = 0; i < nArgNum - 1; i++)
			{
				type		=	msg.readByte();
				ArgValue[i] =	msg.GetDataByType(type);
				
				showmsg += ArgValue[i] + " ";
			}
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到自定义消息["+command+"],参数："+IO.toString(ArgValue));
			
//			switch(command)
//			{
//				case CustomToClientFlag.SERVER_CUSTOMMSG_SCENE_PLAYER_LIST_ALL:
//					model.scene.AddObjsForCust(ArgValue);
//					break;
//				case CustomToClientFlag.SERVER_CUSTOMMSG_SCENE_PLAYER_LIST_ADD:
//					model.scene.AddObjForCust(ArgValue);
//					break;
//				case CustomToClientFlag.SERVER_CUSTOMMSG_SCENE_PLAYER_LIST_DEL:
//					model.scene.delObjForCust(ArgValue);
//					break;
//				default:
//					break;
//			}
//			
			switch(command)
			{
				case CustomToClientFlag.SERVER_CUSTOMMSG_ADD_OBJECT:
					//添加对象
					model.scene.AddObjByCustom(ArgValue);
					break;
				case CustomToClientFlag.SERVER_CUSTOMMSG_OBJECT_PROPERTY_CHANGE:
					//显示对象属性修改
					model.scene.UpdataObjByCustom(ArgValue);
					break;
				case CustomToClientFlag.SERVER_CUSTOMMSG_OBJECT_DEL:
					//修改显示对象
					model.scene.DelObjByCustom(ArgValue);
					break
				default:
					break;
			}
			
			//给NetMgr发消息,用command作为event
			model.dispatchEvent(new CustomMsgEvent(String(command),ArgValue));
			
//			IO.traceLog("自定消息->",command);
		}
		
		/**
		 * 接受GM求助。 
		 * @param e
		 * 
		 */		
		private function ServerFromGM(e:MsgEvent):void
		{
			// 参数数据
			var msg		:ByteArrayEx	= e._msgData;			
			var gmID	:int	=	msg.readUnsignedInt();
			var gmName	:String	=	msg.readWStringByLength(66);
			
			var gmChat	:String =	msg.readWStringEx();
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_FROM_GM("+e.type+")],gmID:"+gmID+
					",gmName:"+gmName+",gmChat:"+gmChat);
			
			//模拟聊天频道
			model.dispatchEvent(new CustomMsgEvent(String(CustomToClientFlag.SERVER_CUSTOMMSG_CHAT),[GameDefine.CHATTYPE_GM_HELP, gmName, gmChat]));
		}
		
		/**
		 * 系统提示 
		 * @param e
		 * 
		 */		
		private function ServerAlert(e:MsgEvent):void
		{
			//_UI16	nInfoType;		// 信息类型
			//string	szInfo;		// 文字信息ID
			//_UI08		nArgNum;	// 参数数量
			//_UI08		nArgType;	// 参数类型
			//Var					// 参数	
			
			var msg			:ByteArrayEx	=	e._msgData;			
			var nInfoType	:uint			=	msg.readUnsignedShort();			
			var strID		:String			=	msg.readMultiByteEx(true);
			var argcount	:int			=	msg.readByte();
			var	type		:int;
			var	argArr		:Array			= 	new Array();
			var data		:*;
			
			
			for(var i:int	=	0;	i < argcount; i++)
			{
				type	=	msg.readByte();
				data	=	msg.GetDataByType(type);
				
				if(type	==	NetDataType.VISTYPE_WIDESTR)
				{
					data	=	new WString(data);
				}
				argArr.push(data);
			}
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_SYSTEM_INFO_NEW("+e.type+")],nInfoType:"+nInfoType+
					",strID:"+strID+",argcount:"+argcount+",argArr:"+IO.toString(argArr));
			
			AlertLogic.getInstance().AddAlert(nInfoType,strID,argArr);
		}
		
		private function ServerMenu(e:MsgEvent):void
		{
//			UNIQUE_ID	ObjectId;	// 对象唯一标识
//			ASYNC_ID	AsyncId;	// 异步事件标识
//			_UI08		nCount;		// 对话信息数量
//			//_UI08		nType;		// 类型，0纯文字，1过场，2选项
//			//_UI16		nMark;		// 标记
//			//string	szInfo;		// 文字信息ID
//			//_UI08		nArgNum;	// 参数数量
//			//_UI08		nArgType;	// 参数类型
//			//Var					// 参数	
			
			var msg:ByteArrayEx		= e._msgData;
			var ObjectId:Point		= msg.readObjectEx();
			var AsyncId:Point		= msg.readObjectEx();
			var nCount:uint			= msg.readUnsignedByte();
			
			var	arrMenu:Array	= new Array();	
			for(var i:int	=	0;	i < nCount; i++)
			{
				var	nType:int		=	msg.readByte();
				var	nMark:int		=	msg.readUnsignedShort();
				var	szInfo:String	=	msg.readMultiByteEx(true);
				var	nArgNum:int		=	msg.readByte();
				var	argArr:Array	= 	new Array();	
				for(var j:int	=	0;	j < nArgNum; j++)
				{
					var	nArgType:int		=	msg.readByte();
					argArr.push(msg.GetDataByType(nArgType));
				}
				var rowValue:BaseObject	=	new BaseObject();
				rowValue.HandleProperties("nType",nType);
				rowValue.HandleProperties("nMark",nMark);
				rowValue.HandleProperties("szInfo",szInfo);
				rowValue.HandleProperties("argArr",argArr);
				arrMenu.push(rowValue);
			}
			
			if(_isShowLog)
				IO.traceCommand("    ↓		收到引擎消息[SERVER_MENU_NEW("+e.type+")],ObjectId:("+ObjectId.x+","+ObjectId.y+
					"),AsyncId:("+AsyncId.x+","+AsyncId.y+"),arrMenu:"+arrMenu);
			
			//派发添加菜单消息
			//给NetMgr发消息,用command作为event
			model.dispatchEvent(new BaseNetEvent(BaseNetEvent.ADD_MENU,ObjectId,AsyncId,arrMenu));
		}
		private function ServerMenuClear(e:MsgEvent):void
		{
			//暂不处理
		}	
	}
}
